/**
 * @title Cell.java
 * @package com.esun.cheetah.view.custom.calendar
 * @description 
 * @author liugan
 * @date 2012-4-14
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.custom.calendar;

import com.esun.cheetah.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Rect;

/**
 * @title Cell
 * @description
 * @author liugan
 */
public abstract class Cell {

	protected Rect mBound = null;
	protected boolean currentMonth;
	protected int mDayOfMonth = 1; // from 1 to 31
	int dx, dy;
	private boolean pressed = false;
	protected Paint normalPaint;
	protected Paint pressedPaint;
	protected Context context;
	private static NinePatch pressedNinePatch;

	public boolean isCurrentMonth() {
		return currentMonth;
	}

	public void setCurrentMonth(boolean currentMonth) {
		this.currentMonth = currentMonth;
	}

	public boolean isPressed() {
		return pressed;
	}

	public void setPressed(boolean pressed) {
		this.pressed = pressed;
	}

	public Cell(Context context, int dayOfMon, Rect rect) {
		this.context = context;
		mDayOfMonth = dayOfMon;
		mBound = rect;
		normalPaint = getNormalPaint();
		pressedPaint = getPressedPaint();
		if (pressedNinePatch == null) {
			Bitmap mPressedBitmap = BitmapFactory.decodeResource(
					context.getResources(), R.drawable.btn_pressed_time);
			pressedNinePatch = new NinePatch(mPressedBitmap,
					mPressedBitmap.getNinePatchChunk(), null);
		}

		prepareDraw();
	}

	/**
	 * @title getPaint
	 * @description
	 * @return
	 */
	abstract Paint getNormalPaint();

	abstract Paint getPressedPaint();

	protected void prepareDraw() {
		normalPaint.setTextSize(CalendarView.DAY_TEXT_SIZE);
		dx = (int) normalPaint.measureText(String.valueOf(mDayOfMonth)) / 2;
		dy = (int) (-normalPaint.ascent() + normalPaint.descent()) / 2;
	}

	protected void draw(Canvas canvas) {
		drawNormalBackground(canvas);
		if (pressed) {
			drawPressedBackground(canvas);
			canvas.drawText(
					String.valueOf(mDayOfMonth),
					mBound.centerX() - dx,
					mBound.centerY() - CalendarView.TEXT_HEIGHT_DAY_OFFSET + dy,
					pressedPaint);
		} else {
			canvas.drawText(
					String.valueOf(mDayOfMonth),
					mBound.centerX() - dx,
					mBound.centerY() - CalendarView.TEXT_HEIGHT_DAY_OFFSET + dy,
					normalPaint);
		}

	}

	/**
	 * @title drawNormalBackground
	 * @description
	 */
	protected void drawNormalBackground(Canvas canvas) {
		// TODO Auto-generated method stub

	}

	/**
	 * @title drawPressedBackground
	 * @description
	 */
	protected void drawPressedBackground(Canvas canvas) {
		pressedNinePatch.draw(canvas, mBound);
	}

	public int getDayOfMonth() {
		return mDayOfMonth;
	}

	public boolean hitTest(int x, int y) {
		return mBound.contains(x, y);
	}

	public Rect getBound() {
		return mBound;
	}

	public String toString() {
		return String.valueOf(mDayOfMonth) + "(" + mBound.toString() + ")";
	}

}
